home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / filenames.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-20  |  9.4 KB  |  485 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. /*********************************************************************/
  28. /*                                                                   */
  29. /*  TOOLKITS: "sharable" library                                     */
  30. /*                                                                   */
  31. /*********************************************************************/
  32.  
  33. #include "cf.defs.h"
  34. #include "cf.extern.h"
  35.  
  36. /*********************************************************************/
  37. /* Level 1                                                           */
  38. /*********************************************************************/
  39.  
  40. IsAbsoluteFileName(f)
  41.  
  42. char *f;
  43.  
  44. {
  45. if (*f == '/' || *f == '.')
  46.    {
  47.    return true;
  48.    }
  49.  
  50. return false;
  51. }
  52.  
  53. /*******************************************************************/
  54.  
  55. AddSlash(str)
  56.  
  57. char *str;
  58.  
  59. {
  60. if ((strlen(str)== 0) || (str == NULL))
  61.    {
  62.    return;
  63.    }
  64.  
  65. if (str[strlen(str)-1] != '/')
  66.    {
  67.    strcat(str,"/");
  68.    }
  69. }
  70.  
  71. /*********************************************************************/
  72.  
  73. DeleteSlash(str)
  74.  
  75. char *str;
  76.  
  77. {
  78. if ((strlen(str)== 0) || (str == NULL))
  79.    {
  80.    return;
  81.    }
  82.  
  83.  if (str[strlen(str)-1] == '/')
  84.    {
  85.    str[strlen(str)-1] = '\0';
  86.    }
  87. }
  88.  
  89. /*********************************************************************/
  90.  
  91. DeleteNewline(str)
  92.  
  93. char *str;
  94.  
  95. {
  96. if ((strlen(str)== 0) || (str == NULL))
  97.    {
  98.    return;
  99.    }
  100.  
  101.  if (str[strlen(str)-1] == '\n')
  102.    {
  103.    str[strlen(str)-1] = '\0';
  104.    }
  105. }
  106.  
  107. /*********************************************************************/
  108.  
  109. ChopLastNode(str)
  110.  
  111.   /* Chop off trailing node name (possible blank) starting from
  112.      last character and removing up to the first / encountered 
  113.      e.g. /a/b/c -> /a/b
  114.           /a/b/ -> /a/b                                        */
  115.  
  116. char *str;
  117.  
  118. { char *sp;
  119.  
  120. if ((sp = strrchr(str, '/')) == NULL)
  121.    {
  122.    return false;
  123.    }
  124. else
  125.    {
  126.    *sp = '\0';
  127.    return true;
  128.    }
  129. }
  130.  
  131. /*********************************************************************/
  132.  
  133. char *CanonifyName(str)
  134.  
  135. char *str;
  136.  
  137. { static char buffer[bufsize];
  138.   char *sp;
  139.  
  140. bzero(buffer,bufsize);
  141. strcpy(buffer,str);
  142.  
  143. for (sp = buffer; *sp != '\0'; sp++)
  144.     {
  145.     if (!isalnum(*sp))
  146.        {
  147.        *sp = '_';
  148.        }
  149.     }
  150.  
  151. return buffer;
  152. }
  153.  
  154. /*********************************************************************/
  155.  
  156. char *ASUniqueName(str) /* generates a unique action sequence name */
  157.  
  158. char *str;
  159.  
  160. { static char buffer[bufsize];
  161.   struct Item *ip;
  162.   char *sp;
  163.  
  164. bzero(buffer,bufsize);
  165. strcpy(buffer,str);
  166.  
  167. for (ip = VADDCLASSES; ip != NULL; ip=ip->next)
  168.    {
  169.    if (strlen(buffer)+strlen(ip->name)+3 > maxlinksize)
  170.       {
  171.       break;
  172.       }
  173.    
  174.    strcat(buffer,".");
  175.    strcat(buffer,ip->name);
  176.    }
  177.  
  178. return buffer;
  179. }
  180.  
  181. /*********************************************************************/
  182.  
  183. char *ReadLastNode(str)
  184.  
  185. /* Return the last node of a pathname string  */
  186.  
  187. char *str;
  188.  
  189. { char *sp;
  190.   
  191. if ((sp = strrchr(str, '/')) == NULL)
  192.    {
  193.    return str;
  194.    }
  195. else
  196.    {
  197.    return sp + 1;
  198.    }
  199. }
  200.  
  201. /*********************************************************************/
  202.  
  203. MakeDirectoriesFor(file)  /* Make all directories which underpin file */
  204.  
  205. char *file;
  206.  
  207. { char *sp,*spc;
  208.   char currentpath[bufsize];
  209.   char pathbuf[bufsize];
  210.   struct stat statbuf;
  211.  
  212. if (*file != '/')
  213.    {
  214.    sprintf(OUTPUT,"Will not create directories for a relative filename %s\nHas no invariant meaning\n",file);
  215.    CfLog(cferror,OUTPUT,"");
  216.    return false;
  217.    }
  218.  
  219. strcpy(pathbuf,file);                                                    /* local copy */
  220.  
  221. for (sp = pathbuf+strlen(file); (*sp != '/') && (sp >= pathbuf); sp--)   /* skip link name */
  222.    {
  223.    *sp = '\0';
  224.    }
  225.  
  226. if (lstat(pathbuf,&statbuf) != -1)
  227.    {
  228.    if (S_ISLNK(statbuf.st_mode))
  229.       {
  230.       Verbose("%s: INFO: %s is a symbolic link, not a true directory!\n",VPREFIX,pathbuf);
  231.       }
  232.  
  233.    if (! S_ISLNK(statbuf.st_mode) && ! S_ISDIR(statbuf.st_mode))
  234.       {
  235.       sprintf(OUTPUT,"Warning. The object %s is not a directory.\n",pathbuf);
  236.       CfLog(cfinform,OUTPUT,"");
  237.       CfLog(cfinform,"Cannot make a new directory without deleting it!\n\n","");
  238.       return(false);
  239.       }
  240.    }
  241.  
  242. for (sp = file, spc = currentpath; *sp != '\0'; sp++)
  243.    {
  244.    if (*sp != '/' && *sp != '\0')
  245.       {
  246.       *spc = *sp;
  247.       spc++;
  248.       }
  249.    else
  250.       {
  251.       *spc = '\0';
  252.  
  253.       if (strlen(currentpath) == 0)
  254.          {
  255.          }
  256.       else if (stat(currentpath,&statbuf) == -1)
  257.          {
  258.          Debug2("cfengine: Making directory %s, mode %o\n",currentpath,DEFAULTMODE);
  259.  
  260.          if (! DONTDO)
  261.             {
  262.             if (mkdir(currentpath,DEFAULTMODE) == -1)
  263.                {
  264.                sprintf(OUTPUT,"Unable to make directories to %s\n",file);
  265.                CfLog(cferror,OUTPUT,"mkdir");
  266.                return(false);
  267.                }
  268.             }
  269.          }
  270.       else
  271.          {
  272.          if (! S_ISDIR(statbuf.st_mode))
  273.             {
  274.             sprintf(OUTPUT,"Cannot make %s. %s is not a directory!\n",file,currentpath);
  275.         CfLog(cferror,OUTPUT,"");
  276.             return(false);
  277.             }
  278.          }
  279.  
  280.       *spc = '/';
  281.       spc++;
  282.       }
  283.    }
  284.  
  285. Debug("Directory for %s exists. Okay\n",file);
  286. return(true);
  287. }
  288.  
  289. /*********************************************************************/
  290.  
  291. BufferOverflow(str1,str2)                   /* Should be an inline ! */
  292.  
  293. char *str1, *str2;
  294.  
  295. { int len = strlen(str2);
  296.  
  297. if ((strlen(str1)+len) > (bufsize - buffer_margin))
  298.    {
  299.    sprintf(OUTPUT,"Buffer overflow constructing string. Increase bufsize macro.\n");
  300.    CfLog(cferror,OUTPUT,"");
  301.    printf("%s: Tried to add %s to %s\n",VPREFIX,str2,str1);
  302.    return true;
  303.    }
  304.  
  305. return false;
  306. }
  307.  
  308. /*********************************************************************/
  309.  
  310. Chop(str)
  311.  
  312. char *str;
  313.  
  314. {
  315. if ((str == NULL) || (strlen(str)==0))
  316.    {
  317.    return;
  318.    }
  319.  
  320. str[strlen(str)-1] = '\0';
  321. }
  322.  
  323.  
  324.  
  325. /*********************************************************************/
  326.  
  327. CompressPath(dest,src)
  328.  
  329. char *dest, *src;
  330.  
  331. { char *sp;
  332.   char node[bufsize];
  333.  
  334. Debug2("CompressPath(%s,%s)\n",dest,src);
  335.  
  336. bzero(dest,bufsize);
  337.  
  338. for (sp = src; *sp != '\0'; sp++)
  339.    {
  340.    if (*sp == '/')
  341.       {
  342.       continue;
  343.       }
  344.  
  345.    bzero(node,maxlinksize);
  346.  
  347.    sscanf(sp,"%[^/]",node);
  348.  
  349.    if (strlen(node) > maxlinksize)
  350.       {
  351.       CfLog(cferror,"Link in path suspiciously large","");
  352.       return false;
  353.       }
  354.    
  355.    sp += strlen(node) - 1;
  356.  
  357.    if (strcmp(node,".") == 0)
  358.       {
  359.       continue;
  360.       }
  361.  
  362.    if (strcmp(node,"..") == 0)
  363.       {
  364.       if (! ChopLastNode(dest))
  365.      {
  366.      Debug("cfengine: used .. beyond top of filesystem!\n");
  367.      return false;
  368.      }
  369.       continue;
  370.       }
  371.    else
  372.       {
  373.       strcat(dest,"/");
  374.       }
  375.    
  376.    if (BufferOverflow(dest,node))
  377.       {
  378.       return false;
  379.       }
  380.    
  381.    strcat(dest,node);
  382.    }
  383. return true;
  384. }
  385.  
  386. /*********************************************************************/
  387. /* TOOLKIT : String                                                  */
  388. /*********************************************************************/
  389.  
  390. char ToLower (ch)
  391.  
  392. char ch;
  393.  
  394. {
  395. if (isdigit(ch) || ispunct(ch))
  396.    {
  397.    return(ch);
  398.    }
  399.  
  400. if (islower(ch))
  401.    {
  402.    return(ch);
  403.    }
  404. else
  405.    {
  406.    return(ch - 'A' + 'a');
  407.    }
  408. }
  409.  
  410.  
  411. /*********************************************************************/
  412.  
  413. char ToUpper (ch)
  414.  
  415. char ch;
  416.  
  417. {
  418. if (isdigit(ch) || ispunct(ch))
  419.    {
  420.    return(ch);
  421.    }
  422.  
  423. if (isupper(ch))
  424.    {
  425.    return(ch);
  426.    }
  427. else
  428.    {
  429.    return(ch - 'a' + 'A');
  430.    }
  431. }
  432.  
  433. /*********************************************************************/
  434.  
  435. char *ToUpperStr (str)
  436.  
  437. char *str;
  438.  
  439. { static char buffer[bufsize];
  440.   int i;
  441.  
  442. bzero(buffer,bufsize);
  443.   
  444. if (strlen(str) >= bufsize)
  445.    {
  446.    FatalError("String too long in ToUpperStr: %s",str);
  447.    }
  448.  
  449. for (i = 0; str[i] != '\0'; i++)
  450.    {
  451.    buffer[i] = ToUpper(str[i]);
  452.    }
  453.  
  454. buffer[i] = '\0';
  455.  
  456. return buffer;
  457. }
  458.  
  459.  
  460. /*********************************************************************/
  461.  
  462. char *ToLowerStr (str)
  463.  
  464. char *str;
  465.  
  466. { static char buffer[bufsize];
  467.   int i;
  468.  
  469. bzero(buffer,bufsize);
  470.  
  471. if (strlen(str) >= bufsize)
  472.    {
  473.    FatalError("String too long in ToUpperStr: %s",str);
  474.    }
  475.  
  476. for (i = 0; str[i] != '\0'; i++)
  477.    {
  478.    buffer[i] = ToLower(str[i]);
  479.    }
  480.  
  481. buffer[i] = '\0';
  482.  
  483. return buffer;
  484. }
  485.